/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import javax.swing.JOptionPane;

/**
 * Classe que corresponde a tela de cadastro de livro do sistema, usando interface grafica.
 * @author alisson
 */
public class TelaCadastroDeLivro extends javax.swing.JFrame {
	
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoAutor;
    private javax.swing.JTextArea campoDescricao;
    private javax.swing.JTextField campoTema;
    private javax.swing.JTextField campoTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    
    private String txtTitulo, txtAutor, txtDescricao, txtTema;
    private Professor proprietario;
 

    /**
     * Cria uma tela de cadastro de livros.
     */
    public TelaCadastroDeLivro(Professor proprietario) {
    	this.proprietario = proprietario;
        initComponents();
    }

    /**
     * Componentes que compoe a tela de cadastro de livros e seus campos, onde o usuario digita seus dados.
     */
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        botaoSalvar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        campoTitulo = new javax.swing.JTextField();
        campoAutor = new javax.swing.JTextField();
        campoTema = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDescricao = new javax.swing.JTextArea();
        

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de livro - Biblioteca");
        setResizable(false);

        jLabel1.setText("Titulo");

        jLabel2.setText("Autor");

        jLabel3.setText("Tema");

        jLabel4.setText("Descrição");

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					botaoSalvarActionPerformed(evt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        campoTitulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoTituloActionPerformed(evt);
            }
        });

        campoAutor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoAutorActionPerformed(evt);
            }
        });

        campoTema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoTemaActionPerformed(evt);
            }
        });

        campoDescricao.setColumns(20);
        campoDescricao.setRows(5);
        campoDescricao.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                campoDescricaoFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(campoDescricao);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoTema, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoAutor, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(62, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(botaoCancelar)
                        .addGap(122, 122, 122))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoTema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botaoCancelar)
                    .addComponent(botaoSalvar))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * Configura o que o usuario digitou para o campo autor.
     * @param evt
     * 		Parametro a ser configurado para o campo autor.
     */
    private void campoAutorActionPerformed(java.awt.event.ActionEvent evt) {
        this.txtAutor = campoAutor.getText();
    }
    
    /**
     * Configura o que o usuario digitou para o campo titulo.		
     * @param evt
     * 		Parametro a ser configurado para o campo titulo.
     */
    private void campoTituloActionPerformed(java.awt.event.ActionEvent evt) {
        this.txtTitulo = campoTitulo.getText();
    }

    /**
     * Configura o que o usuario digitou para o campo tema.
     * @param evt
     * 		Parametro a ser configurado para o campo tema.
     */
    private void campoTemaActionPerformed(java.awt.event.ActionEvent evt) {
        this.txtTema = campoTema.getText();
    }
    
    /**
     * Configura o que o usuario digitou para o campo descricao.
     * @param evt
     * 		Parametro a ser configurado para o campo descricao.
     */
    private void campoDescricaoFocusLost(java.awt.event.FocusEvent evt) {
        this.txtDescricao = campoDescricao.getText();
    }
    
    /**
     * Salva os dados que o usuario digitou nos campos da tela de cadastro de livro.
     * @param evt
     * 		Parametro que serve de controle para manusear os metodos da biblioteca.
     * @throws Exception
     * 		Lanca excecao, caso o usuario digite alguma campo de maneira errada.
     */
    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {
    	
    	System.out.println(proprietario.bibliotecaParticular);
        if (txtAutor.length() > 0 && txtTema.length() > 0 && txtTitulo.length() > 0) {
        	try {
        		proprietario.adicionaLivro(new Livro(txtTitulo, txtAutor, txtTema, proprietario.getSala()));
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, e.getMessage());	
			}
			
			
		}
    }
    
    /**
     * Cancela a operacao.
     * @param evt
     * 		Parametro que serve de controle para manusear os metodos da biblioteca.
     */
    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

}
